layui.define(['form','table'], function(exports){
    var $ = layui.$,form = layui.form,table = layui.table;

    var userId = $('input[name="userId"]').val();

    table.render({
        elem: '#LAY-table'
        ,url: layui.setter.webRoot + '/agent/relation/user/list.data'
        ,where:{userId:userId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'relationUserId', title: '用户ID',width:80}
            ,{field: 'name', title: '用户名称',templet:function (record){
                if(record.relationUser){
                    return record.relationUser.name;
                }else{
                    return '未知用户信息';
                }
            }}
            ,{field: 'phone', title: '用户手机号',templet:function (record){
                if(record.relationUser){
                    return record.relationUser.phone;
                }else{
                    return '--';
                }
            }}
            ,{field: 'roleTypeName', title: '角色',width:100}
            ,{field: 'placeCount', title: '网点数量',width:100}
            ,{field: 'deviceCount', title: '设备数量',width:100}
            ,{field: 'funcModelCount', title: '功能模块',width:100}
            ,{field: 'createDate', title: '创建时间'}
            ,{title: '操作', width: 180, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
        ]]
        ,page: false
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    var active = {
        info:function (obj){
            parent.layer.open({
                type: 2
                ,title: obj.data.name
                ,content: layui.setter.webRoot + '/agent/relation/user/info.html?userId='+obj.data.userId+'&relationUserId='+ obj.data.relationUserId
                ,maxmin: true
                ,area: ['90%', '90%']
                ,btn: ['取消']
            });
        }
        ,walletInfo:function (obj){
            parent.layer.open({
                type: 2
                ,title: obj.data.name
                ,content: layui.setter.webRoot + '/agent/relation/wallet/info.html?userId='+obj.data.userId+'&relationUserId='+ obj.data.relationUserId
                ,maxmin: true
                ,area: ['80%', '80%']
                ,btn: ['取消']
            });
        }
        ,del:function(obj){
            layer.confirm('确定要删除该用户关系吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/agent/relation/user/del.do',
                    data:{userId:userId,relationUserId:obj.data.relationUserId},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            layer.msg('删除成功',{icon: 1});
                            table.reload('LAY-table');
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    };

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.on('tool(LAY-table)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });

    exports('agent_relation_info', {})
});